/**
 * @Title: LeaveOutAdapter.java
 * @Package: com.jason.bluetooth.fingerprint.adapter
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月9日23:32:04
 * @Version: V1.0
 */
package com.jason.platform.supervision.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.beans.WorkerBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: CommunicationAdapter
 * @Description: 通讯录 适配器
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-8-8 15:23:10
 */
public class CommunicationAdapter extends BaseAdapter {

	private static final String TAG = CommunicationAdapter.class.getSimpleName();

	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private List<WorkerBean> mDatas = new ArrayList<WorkerBean>();

	public CommunicationAdapter(Context context) {
		super();
		this.mContext = context;
		this.mLayoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mDatas = new ArrayList<WorkerBean>();
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void addAll(List<WorkerBean> datas) {
		this.mDatas = datas;
		if (mDatas == null){
			mDatas = new ArrayList<WorkerBean>();
		}
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.adapter_communication, null);
			holder = new Holder();
			holder.mNameTextView = (TextView) convertView
					.findViewById(R.id.communication_name);
			holder.mPhoneTextView = (TextView) convertView
					.findViewById(R.id.communication_phone);
			holder.mIdentityTextView = (TextView) convertView
					.findViewById(R.id.communication_identity);
			holder.mViewTextView = (TextView) convertView
					.findViewById(R.id.communication_view);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final WorkerBean bean = (WorkerBean) getItem(position);
		if (bean != null){
			holder.mNameTextView.setText(bean.getUserName());
			holder.mPhoneTextView.setText(bean.getTelephone());
			holder.mIdentityTextView.setText(bean.getRoleName());
		}

		holder.mViewTextView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//TODO 拨打手机号码
				String phone = bean != null ? bean.getTelephone() : "";
				if (!TextUtils.isEmpty(phone)){
					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
					mContext.startActivity(intent);
				}else {
					Toast.makeText(mContext, "没有手机号码无法拨打", Toast.LENGTH_SHORT).show();
				}
			}
		});

		return convertView;
	}

	class Holder {
		TextView mNameTextView;
		TextView mPhoneTextView;
		TextView mIdentityTextView;//人员身份
		TextView mViewTextView;
	}

}
